package org.example.mybatis3.controller;

import org.example.mybatis3.entity.User;
import org.example.mybatis3.service.UserXmlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users_xml")
public class UserXmlController {

    @Autowired
    private UserXmlService userService;

    @GetMapping
    public List<User> findAll() {
        return userService.findAll();
    }

    @GetMapping("/{id}")
    public User findById(@PathVariable Long id) {
        return userService.findById(id);
    }

    @GetMapping("/cons/{id}")
    public User findByIdWithResultMapConstructor(@PathVariable Long id) {
        return userService.findByIdWithResultMapConstructor(id);
    }

    @GetMapping("/map/{id}")
    public Map<String, Object> findByIdWithMap(@PathVariable Long id) {
        return userService.findByIdWithMap(id);
    }

    @GetMapping("/pojo/{id}")
    public User findByIdWithPojo(@PathVariable Long id) {
        return userService.findByIdWithPojo(id);
    }

    @PostMapping
    public String save(@RequestBody User user) {
        int result = userService.save(user);
        return result > 0 ? "保存成功"+result : "保存失败";
    }

    @DeleteMapping("/{id}")
    public String delete(@PathVariable Long id) {
        int result = userService.deleteById(id);
        return result > 0 ? "删除成功" : "删除失败";
    }
}
